package com.cnhis.cloudhealth.clidoctorweb.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ResourceBundle;

/**
 * 提供数据库连接池 和数据库连接
 * 方法为静态的 通过类型访问
 * @author huhongda
 * 
 */
public class JdbcUtils {

    private static String driverName = null;
    private static String url=null;
    private static String userName = null;
    private static String userPwd =null;
    //读取配置文件中的内容 
    //静态方法块
    static{
//        ResourceBundle bundle = ResourceBundle.getBundle("dataBase");
         driverName = "org.postgresql.Driver";
         url = "jdbc:postgresql://192.168.1.18:5432/cloudhealth";
         userName= "postgres";
         userPwd = "cloudhealth";
    }
    //建立连接
    public static Connection getConnection(){
        try {
            //注册驱动
            loadDriver();
            //System.out.println("创建连接成功");
            //建立连接 并返回
            return DriverManager.getConnection(url, userName, userPwd);

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;
    }
    //注册驱动
    public static void loadDriver(){
        try {
            Class.forName(driverName);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    //释放资源
    public static void release(ResultSet rs,Statement stat,Connection con){
        //存在连接或结果集的时候 释放
        if(rs!=null){
            try {
                rs.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
            rs= null;
        }
        if(stat!=null){
            try {
                stat.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
            stat = null;
        }
        if(con!=null){
            try {
                con.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
            con = null;
        }
    }
}